home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / c / System.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  3KB  |  180 lines

  1. /* $Id: System.c,v 1.6 1992/05/05 13:19:05 grosch rel $ */
  2.  
  3. /* $Log:
  4.  */
  5.  
  6. /* Ich, Doktor Josef Grosch, Informatiker, Jan. 1992 */
  7.  
  8. /* interface for machine dependencies */
  9.  
  10. /* See the header file System.h for comments concerning the external routines!    */
  11.  
  12. /* compilation with the option -DUNIX uses UNIX system calls for IO (efficient),
  13.    otherwise the C library routines are used for IO (portable).            */
  14.  
  15. static char rcsid [] = "$Id: System.c,v 1.6 1992/05/05 13:19:05 grosch rel $";
  16.  
  17. # include "System.h"
  18.  
  19. # ifdef m68000
  20. # define hz 50
  21. # else
  22. # define hz 60
  23. # endif
  24.  
  25. # ifndef UNIX
  26.  
  27. # include <stdio.h>
  28. # define NOFILES 32
  29.  
  30. static char IsLineBuffered [NOFILES] = { 1, 1, 1, };
  31.  
  32. static FILE *    FileStore [NOFILES] = {
  33.    stdin, stdout, stderr, NULL, NULL, NULL, NULL, NULL,
  34.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  35.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  36.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  37. };
  38.  
  39. static tFile FileToInt (File)
  40.    FILE *    File;
  41. {
  42.    register int    f = fileno (File);
  43.    FileStore [f] = File;
  44.    return f;
  45. }
  46.  
  47. static FILE * IntToFile (File)
  48.    tFile    File;
  49. {
  50.    return FileStore [File];
  51. }
  52.  
  53. # endif
  54.  
  55. /* binary IO */
  56.  
  57. # include <fcntl.h>
  58. # include <sys/types.h>
  59. # include <sys/stat.h>
  60.  
  61. bool IsCharacterSpecial (File) tFile File;
  62. {
  63.    struct stat    buf;
  64.    (void) fstat (File, & buf);
  65.    return (0020000 & buf.st_mode) == 0020000;
  66. }
  67.  
  68. tFile OpenInput (FileName)
  69.    char *    FileName;
  70. {
  71. # ifndef UNIX
  72.    tFile File = FileToInt (fopen (FileName, "r"));
  73.    IsLineBuffered [File] = IsCharacterSpecial (File);
  74.    return File;
  75. # else
  76.    return open (FileName, O_RDONLY);
  77. # endif
  78. }
  79.  
  80. tFile OpenOutput (FileName)
  81.    char *    FileName;
  82. {
  83. # ifndef UNIX
  84.    return FileToInt (fopen (FileName, "w"));
  85. # else
  86.    return creat (FileName, 0666);
  87. # endif
  88. }
  89.  
  90. int Read (File, Buffer, Size)
  91.    tFile    File;
  92.    char *    Buffer;
  93.    int        Size;
  94. {
  95. # ifndef UNIX
  96.    if (IsLineBuffered [File]) {
  97.       Buffer [0] = '\0';
  98.       (void) fgets (Buffer, Size, IntToFile (File));
  99.       return strlen (Buffer);
  100.    } else
  101.       return fread (Buffer, 1, Size, IntToFile (File));
  102. # else
  103.    return read (File, Buffer, Size);
  104. # endif
  105. }
  106.  
  107. int Write (File, Buffer, Size)
  108.    tFile    File;
  109.    char *    Buffer;
  110.    int        Size;
  111. {
  112. # ifndef UNIX
  113.    return fwrite (Buffer, 1, Size, IntToFile (File));
  114. # else
  115.    return write (File, Buffer, Size);
  116. # endif
  117. }
  118.  
  119. void Close (File)
  120.    tFile    File;
  121. {
  122. # ifndef UNIX
  123.    (void) fclose (IntToFile (File));
  124. # else
  125.    (void) close (File);
  126. # endif
  127. }
  128.  
  129. /* calls other than IO */
  130.  
  131. /* # include <malloc.h> */
  132.  
  133. char * SysAlloc (ByteCount) long ByteCount; { return (char *) malloc ((unsigned) ByteCount); }
  134.  
  135. # include <sys/times.h>
  136.  
  137. long Time ()
  138. {
  139.    struct tms    buffer;
  140.    (void) times (& buffer);
  141.    return (buffer.tms_utime + buffer.tms_stime) * 1000 / hz;
  142. }
  143.  
  144. static int    argc;
  145. static char * *    argv;
  146.  
  147. int GetArgCount ()
  148. {
  149.    return argc;
  150. }
  151.  
  152. void GetArgument (ArgNum, Argument)
  153.    int        ArgNum;
  154.    char *    Argument;
  155. {
  156.    register int    i = 0;
  157.    for (;; i ++)
  158.       if ((Argument [i] = argv [ArgNum][i]) == '\0') return;
  159. }
  160.  
  161. void PutArgs (Argc, Argv)
  162.    int        Argc;
  163.    char * *    Argv;
  164. {
  165.    argc = Argc;
  166.    argv = Argv;
  167. }
  168.  
  169. # include <errno.h>
  170.  
  171. int ErrNum () { return errno; }
  172.  
  173. int System (String) char * String; { return system (String); }
  174.  
  175. extern void exit ();
  176.  
  177. void Exit (Status) int Status; { exit (Status); }
  178.  
  179. void BEGIN_System () {}
  180.